package com.bw.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.alibaba.excel.EasyExcel;
import com.bw.Mycommon.Mycommon;
import com.bw.config.UploadMinioUtils;


import com.bw.domain.Menu;
import com.bw.domain.Zxt;
import com.bw.service.MenuService;
import com.bw.service.TuService;
import com.bw.utils.Result;
import com.ctc.wstx.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system")
public class MenuController {

    @Autowired
    MenuService menuService;

    @Autowired
    HttpServletResponse response;


    @Autowired
    TuService tuService;


    @RequestMapping("/exportExecel")
    @Mycommon(value = "导出",type = 0)
    public   void getexport() throws IOException {
        String fileName = URLEncoder.encode("菜单表", "UTF-8").replaceAll("\\+", "%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Menu> list = menuService.getlist();
        EasyExcel.write(response.getOutputStream(), Menu.class).sheet("部门信息").doWrite(list);
    }


    @RequestMapping("/list")
    public Result getlist()
    {

        List<Menu> getlist=menuService.getlist();

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Menu, String>build(getlist, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());

                    // 扩展属性 ...
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("component", treeNode.getComponent());
                });

        return Result.success(treeNodes);

    }


    @RequestMapping("/list2")
    @Mycommon(value = "列表",type = 0)
    public Result getlist2(@RequestBody Menu menu)
    {
        List<Menu> getlist=menuService.getlist2(menu);

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<Menu, String>build(getlist, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());
                    // 扩展属性 ...
                    tree.putExtra("pic", treeNode.getPic());
                    tree.putExtra("orderNum", treeNode.getOrderNum());
                    tree.putExtra("path", treeNode.getPath());
                    tree.putExtra("menuType", treeNode.getMenuType());
                    tree.putExtra("visible", treeNode.getVisible());
                    tree.putExtra("perms", treeNode.getPerms());
                    tree.putExtra("createTime", DateUtil.format(treeNode.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));

                });

        return Result.success(treeNodes);

    }
    @RequestMapping("/getupdateMenu")
    @Mycommon(value = "修改",type = 2)
    public Result getupdateMenu(Integer id)
    {
       Menu menu= menuService.getupdateMenu(id);
        return Result.success(menu);
    }
    @RequestMapping("/updateMenu")
    @Mycommon(value = "修改",type = 2)
    public Result updateMenu(Integer id)
    {
       Menu menu= menuService.updateMenu(id);
        return Result.success(menu);
    }

    @RequestMapping("/getstatus")
    @Mycommon(value = "状态" ,type = 0)
    public Result getstatus()
    {
        List<Menu> list=menuService.getstatus();
        return Result.success(list);
    }


    @RequestMapping("/gettu")
    public Result gettu()
    {
      List<Map<String,String>> list=  menuService.list();
      return Result.success(list);
    }

    @RequestMapping("/addMenu")
    @Mycommon(value = "添加",type = 1)
    public Result addMenu(@RequestBody Menu menu)
    {

        menuService.addMenu(menu);
        return Result.success();
    }

    @RequestMapping("/deleteMenu")
    @Mycommon(value = "删除",type = 3)
    public Result getdelete(Integer id)
    {
        if(menuService.deleteMenu(id))
        {
            return Result.success();
        }
        else
        {
            return Result.fail("删除失败");
        }
    }


    @RequestMapping("/import")
    public  HashMap<String, Object>  getimport(MultipartFile file) throws IOException {

        HashMap<String, Object> hashMap = new HashMap<>();

        try {
            EasyExcel.read(file.getInputStream(), Menu.class, new CellDataDemoHeadDataListener(menuService)).sheet().doRead();
            hashMap.put("msg","成功");
        }catch (Exception e)
        {
            e.printStackTrace();
            hashMap.put("msg","失败");

        }
        return hashMap;
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile file)
    {
        String s = UploadMinioUtils.uploadPic(file);
        return Result.success(s);
    }





}
